﻿using ConfExpert.Datasets;
using ConfExpert.Utility;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Grid;
using DevTeam;
using System;
using System.Collections.Generic;
using System.Drawing;

namespace ConfExpert.Forms.Depo.Kauf
{
    public partial class SatirParcala : XPopupForm
    {
        MyData.OrdersRow row;
        public SatirParcala(MyData.OrdersRow row)
        {
            this.row = row;
            InitializeComponent();
        }

        MyData.OrdersDataTable dt = null;
        private void BedenDagilimi_Load(object sender, EventArgs e)
        {

            this.Text = "Beden Dağılımı";
            MyData.DagilimRow r = AppSession.Lookups.Dagilim.FindByDagilimId(row.DagilimId);

            txtInfo.Caption = row.idx + " | " + "Kesim föyü almak istediğiniz miktarları girin.";

            dt = new MyData.OrdersDataTable();
            dt.ImportRow(row);
            dt.Rows.Add(dt.NewRow());

            gv.Init(dt);

            List<string> colNames = new List<string>();
            for (int i = 0; i < dt.Columns.Count + 1; i++)
                colNames.Add(gv.Columns[i].FieldName);

            colNames.Add("ProductId");
            gv.MakeColumnInvisible(colNames.ToArray());

            if (r != null)
            {
                for (int i = 1; i < 29; i++)
                {
                    string fieldName = String.Format("A{0:00}", i);
                    string cap = r[String.Format("A{0:00}", i)].ToString();
                    if (cap != "")
                    {
                        gv.Columns[fieldName].Caption = cap != string.Empty ? cap : " ";
                        gv.Columns[fieldName].VisibleIndex = i;
                    }
                }
            }

            gv.OptionsView.ShowAutoFilterRow = false;
            gv.Appearance.FocusedCell.BackColor = Color.YellowGreen;
            gv.BestFitColumns();
            btnSave.ItemClick += btnSave_ItemClick;
        }

        void btnSave_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            gv.EndEditing();
            string strUpdate = "update Orders set ";

            foreach (GridColumn col in gv.VisibleColumns)
            {
                int val0 = dt.Rows[0][col.FieldName].ToInt();
                int val1 = dt.Rows[1][col.FieldName].ToInt();
                if (val1 > val0)
                {
                    ShowInfoMessage("Ana satırdan fazla adet giremezsiniz.");
                    return;
                }
                else if (val1 > 0)
                {


                    strUpdate += col.FieldName + "=" + val1 + ",";
                }
            }

            strUpdate = strUpdate.RemoveIfEndsWith(",");
            strUpdate += " where ";

            UpdateString = strUpdate;

            this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }

        private void gv_RowCellClick(object sender, RowCellClickEventArgs e)
        {
            if (e.RowHandle == 0)
            {
                gv.FocusedRowHandle = 1;
            }
        }

        public string UpdateString = "";

    }
}
